package lt.laimis.shop.web.action;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;
import org.json.JSONException;
import org.json.JSONArray;
import org.json.JSONObject;
import lt.laimis.shop.service.product.ProductService;
import lt.laimis.shop.service.product.Product;
import lt.laimis.shop.service.node.NodeService;

import java.util.List;
import java.util.ArrayList;
import java.io.StringReader;

/**
 * Created by IntelliJ IDEA.
 * User: laimis
 * Date: Jul 17, 2008
 * Time: 5:07:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class BaseAction implements ActionBean {
    private int totalPrice = 0;
    private int quantity = 1;
    private String nodeId;
    private List<Product> selectedProducts;


    public String getNodeId() {
        return nodeId;
    }

    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    public int getTotalPrice() {
        if (this.totalPrice == 0) {
                this.totalPrice = countTotalPrice();
                getContext().getSessionMap().put("shop_order_total_price",totalPrice);
        }
        return totalPrice;
    }

    public int countTotalPrice() {
        List<Product> list = getSelectedProducts();
        int countedPrice = 0;
        int quantity;
        int pirce;
        if (list != null && list.size() > 0) {
            for (Product item : list) {
                quantity = item.getQuantity();
                pirce = item.getPrice();
                if (quantity != 0 && pirce != 0) {
                    countedPrice =+ quantity * pirce;
                }
            }
        }
        return countedPrice;
    }


    public void setTotalPrice(int totalPrice) {
        this.totalPrice = totalPrice;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    @HandlesEvent("inserProductToSeesion")
    public Resolution insertSelectedProduct() throws JSONException {
        Product product = productService.getProductByNodeId(nodeId);
        List<Product> list = (List<Product>) getContext().getSessionMap().get("shop_order_list");
        if(list == null){
           list = new ArrayList<Product>();
        }
        if (product != null) {
            Product newProduct = new Product(
                    product.getNodeId(),
                    product.getType(),
                    product.getName(),
                    product.getTitle(),
                    product.getParentId(),
                    product.getDescription(),
                    product.getPrice(),
                    product.getNote()
            );
            newProduct.setQuantity(countProduct(product, list));
            list.add(newProduct);
            getContext().getSessionMap().put("shop_order_list",list);
        }
        return getProductsFromSeesion();
    }

    public int countProduct(Product product, List<Product> list) {
        int productQuantity = 0;
        Product foreRemove = null;
        if (list != null) {
            for (Product prod : list) {
                if (prod.getNodeId().equals(product.getNodeId())) {
                    productQuantity = +prod.getQuantity();
                    foreRemove = prod;
                }
            }
            list.remove(foreRemove);
        }
        return productQuantity + product.getQuantity();
    }

    @HandlesEvent("deleteProductFromSeesion")
    public Resolution deleteSelectedProduct() throws JSONException {
        List<Product> list = (List<Product>) getContext().getSessionMap().get("shop_order_list");
        Product deleteProduct = null;
        if (list != null && list.size() > 0) {
            for (Product product : list) {
                if (product.getNodeId().equals(getNodeId())) {
                    deleteProduct = product;
                }
            }
            list.remove(deleteProduct);
        }
        return getProductsFromSeesion();
    }

    public Resolution getProductsFromSeesion() throws JSONException {
        if (selectedProducts == null) {
            this.selectedProducts = (List<Product>) getContext().getSessionMap().get("shop_order_list");
        }
        String resut = prodyctListJSON(selectedProducts);
        return new StreamingResolution("text", new StringReader(resut));
    }

    public String prodyctListJSON(List<Product> productList) throws JSONException {
        JSONArray prodyctList = new JSONArray();
        JSONObject order = new JSONObject();
        order.put("totalPrice", getTotalPrice());
        for (Product item : productList) {
            JSONObject ci = new JSONObject();
            ci.put("nodeId", item.getNodeId());
            ci.put("title", item.getTitle());
            ci.put("price", item.getPrice());
            ci.put("quantity", item.getQuantity());
            prodyctList.put(ci);
        }
        order.put("productList", prodyctList);
        return order.toString();
    }

    public List<Product> getSelectedProducts() {
        if (selectedProducts == null) {
            this.selectedProducts = (List<Product>) getContext().getSessionMap().get("shop_order_list");
        }
        return selectedProducts;
    }

    public void setSelectedProducts(List<Product> selectedProducts) {
        this.selectedProducts = selectedProducts;
    }

    private ShopActionBeanContext context;

    public ShopActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = (ShopActionBeanContext) context;
    }

    @SpringBean
    protected ProductService productService;

    @SpringBean
    protected NodeService nodeService;
}
